package com.tledu.web04.controller;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(urlPatterns = "/ajax")
public class AjaxServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "请求回来的数据");
        resp.getWriter().println(jsonObject.toJSONString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // json
//        BufferedReader reader = req.getReader();
//        StringBuilder result = new StringBuilder();
//        while (true) {
//            String line = reader.readLine();
//            if (line == null) {
//                break;
//            }
//            result.append(line);
//        }
//        // 输出一下
//        resp.setContentType("application/json;charset=utf-8");
//        resp.getWriter().println(result.toString());
//        String name = req.getParameter("name");
//        resp.getWriter().println(name);
        // 文件上传

    }
}
